﻿namespace DCHealthCheck.MvcWeb.Controllers {

    #region Usings
    using DCHealthCheck.Common;
    using DCHealthCheck.DomainModel;
    using DCHealthCheck.MvcWeb.Classes;
    using DCHealthCheck.MvcWeb.Classes.DataServices;
    using DCHealthCheck.MvcWeb.Classes.ViewModels;
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    #endregion

    public class HomeController : BaseController {

        private readonly IRepository<HealthCheckSummary, IEntityManager> HealthCheckSummaryRepository;
        private readonly IHomeDataService HomeDataService;

        public HomeController(IRepository<HealthCheckSummary, IEntityManager> healthCheckSummaryRepository, IHomeDataService homeDataService) {
            this.ControllerName = "Home";
            this.HealthCheckSummaryRepository = healthCheckSummaryRepository;
            this.HomeDataService = homeDataService;
        }

        /// <summary>
        /// Deletes the HealthCheck results for the specified DateTime
        /// </summary>
        /// <param name="date">The DateTime of the HealthCheck </param>
        /// <returns></returns>
        [Authorize(Roles = Global.AdminRole)]
        [HttpPost]
        public ActionResult DeleteResults(string date) {
            bool success = true;
            var dateTime = DateTime.MinValue;

            if (DateTime.TryParseExact(date, "yyyy-MM-dd-HH-mm-ss-FFFFFFF", CultureInfo.CurrentCulture, DateTimeStyles.None, out dateTime)) {

                var healthCheckSummary = this.HealthCheckSummaryRepository.Get(x => x.CheckDateTimeUTC == dateTime)
                    .FirstOrDefault();

                if (healthCheckSummary != null) {
                    healthCheckSummary.DetachedState = DetachedState.Deleted;
                    this.HealthCheckSummaryRepository.Delete(healthCheckSummary);
                    this.HealthCheckSummaryRepository.Save();

                    success &= this.HealthCheckSummaryRepository
                        .Get(x => x.CheckDateTimeUTC == dateTime)
                        .FirstOrDefault() == null;

                }
                else {
                    return new HttpStatusCodeResult(500, string.Format("HealthCheckSummary not found for date: {0}", date));
                }
            }

            return success ? new HttpStatusCodeResult(200, "Ok") : new HttpStatusCodeResult(500, "Error occurred performing the requested action.");
        }

        /// <summary>
        /// Gets the summary text of the specified HealthCheck run
        /// </summary>
        /// <param name="id">The database Id of the summary record</param>
        /// <returns>The summary text</returns>
        [Authorize]
        public ActionResult GetHealthCheckSummary(int id) {

            string summary = "Summary not found!";

            var healthCheckSummary = this.HealthCheckSummaryRepository.Get(x => x.Id == id)
                .FirstOrDefault();

            if (healthCheckSummary != null) {
                summary = healthCheckSummary.Summary.Replace(Environment.NewLine, "<br/>");
            }

            var contentResult = new ContentResult() { Content = summary };
            return contentResult;
        }

        [Authorize]
        public ActionResult Index(HomeViewModel viewModel) {
            this.HomeDataService.ProcessData(viewModel);
            return View("Index", viewModel);
        }

    }
}
